﻿var Url = "AjaxData/CustomOperating.aspx";
//自动义双击方法
function CustomDbClick() {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    var orderCode = record.getData("Code");
    var IsHistory = record.getData("IsHistory");
    if (vid != "") {
        if (orderCode.indexOf('DZ') >= 0) {
            window.parent.$("#tabs").tabs('close', '查看赠送订单');
            var pagetitle = "查看赠送订单";
            parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=4&orderType=1&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
            parent.NeedRefreshPage = this;
        } else {
            if (IsHistory == "1") {
                window.parent.$("#tabs").tabs('close', '查看历史订单');
                var pagetitle = "查看历史订单";
                parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=4&orderType=2&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
                parent.NeedRefreshPage = this;
            } else {
                window.parent.$("#tabs").tabs('close', '查看销售订单');
                var pagetitle = "查看销售订单";
                parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=4&orderType=0&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
                parent.NeedRefreshPage = this;
            }
        }
    }
}
//自定义新增方法
var fnCustomAdd = function () {
    
}
//自定义修改方法
var fnCustomEdit = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    var orderCode = record.getData("Code");
    var type = record.getData("OrderType");
    var IsHistory = record.getData("IsHistory");
    if (vid != "") {
        if (orderCode.indexOf('DZ') >= 0) {
            window.parent.$("#tabs").tabs('close', '赠送订单修改');
            var pagetitle = "赠送订单修改";
            parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=2&orderType=1&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
            parent.NeedRefreshPage = this;
        } else {
            if (type.indexOf('一推') >= 0) {
                window.parent.$("#tabs").tabs('close', '一推订单修改');
                var pagetitle = "一推订单修改";
                parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=2&orderType=0&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
                parent.NeedRefreshPage = this;
            } else {
                if (IsHistory == "1") {
                    window.parent.$("#tabs").tabs('close', '历史订单修改');
                    var pagetitle = "历史订单修改";
                    parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=2&orderType=2&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
                    parent.NeedRefreshPage = this;
                } else {
                    window.parent.$("#tabs").tabs('close', '销售订单修改');
                    var pagetitle = "销售订单修改";
                    parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=2&orderType=0&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
                    parent.NeedRefreshPage = this;
                }
            }
        }
    }
}
//自定义删除方法
var fnCustomDeleteData = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        var SaveUrl = Url + "?r=" + Math.random() + "&action=delete&entity=CrmCustomerOrder&json=&type=1&id=" + vid;
        YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
            success: function (res) {
                try {
                    var str = res.responseText;
                    if (str == "1") {
                        YAHOO.example.container.dialog3.hide();
                        alert("删除成功!");
                        RefreshData();
                        return;
                    } else {
                        alert(str);
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    }
}
//新增一推订单
var fnAddOneOrder = function () {
    window.parent.$("#tabs").tabs('close', '新增一推订单');
    var pagetitle = "新增一推订单";
    parent.addTabs(pagetitle, '../Crm/BasePages/StartWorkflow.aspx?title=' + encodeURI(pagetitle) + '&WorktaskId=c65b59df-bcf5-4900-96ec-11d5d3bdb31f&WorkflowId=aadf933d-7dc6-4cf8-80d0-b73edf04ac66');
    parent.NeedRefreshPage = this;
}
//新增订单(销售)
var fnAddOrder = function () {
    window.parent.$("#tabs").tabs('close', '新增销售订单');
    var pagetitle = "新增销售订单";
    //parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=0&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
    //parent.NeedRefreshPage = this;
    parent.addTabs(pagetitle, '../Crm/BasePages/StartWorkflow.aspx?title=' + encodeURI(pagetitle) + '&WorktaskId=c65b59df-bcf5-4900-96ec-11d5d3bdb31f&WorkflowId=aadf933d-7dc6-4cf8-80d0-b73edf04ac66');
    parent.NeedRefreshPage = this;
}
//新增赠送订单
var fnAddSendOrder = function () {
    window.parent.$("#tabs").tabs('close', '新增赠送订单');
    var pagetitle = "新增赠送订单";
    parent.addTabs(pagetitle, '../Crm/BasePages/StartWorkflow.aspx?title=' + encodeURI(pagetitle) + '&WorktaskId=0abf36c1-5f0f-48df-b973-957b3ac4cd88&WorkflowId=3997ef0f-88ef-4578-9679-d3020a6b14b7');
    parent.NeedRefreshPage = this;
}
//订单变更(销售)
var fnOrderMOdifyField = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    var orderCode = record.getData("Code");
    var type = record.getData("OrderType");
    if (vid != "") {
        var SaveUrl = Url + "?r=" + Math.random() + "&type=1&action=check&entity=CrmCustomerCheckField&json=&id=" + vid;
        YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
            success: function (res) {
                try {
                    var str = res.responseText;
                    if (str == "1") { //新建状态
                        OrderModifys(vid, orderCode, type);
                    } else if (str == "2") { //其它状态
                        OrderModifys2(vid, orderCode, type);
                    } else if (str == "3") {
                        alert("您当前的订单处于 签核中 状态，不能变更!");
                        return;
                    } else if (str == "4") {
                        alert("您当前的订单处于 结案 状态，不能变更!");
                        return;
                    } else if (str == "5") {
                        alert("您当前的订单处于 报废 状态，不能变更!");
                        return;
                    } else if (str == "7") {
                        alert("您当前选中的订单不是销售订单,不能变更!");
                        return;
                    } else {
                        alert(str);
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    }
}
var OrderModifys = function (vid, orderCode, type) { //修改订单所有信息
    if (orderCode.indexOf('DZ') >= 0) {
        window.parent.$("#tabs").tabs('close', '赠送订单变更');
        var pagetitle = "赠送订单变更";
        parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=2&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
        parent.NeedRefreshPage = this;
    } else {
        if (type.indexOf('一推') >= 0) {
            window.parent.$("#tabs").tabs('close', '一推订单变更');
            var pagetitle = "一推订单变更";
            parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=2&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
            parent.NeedRefreshPage = this;
        } else {
            window.parent.$("#tabs").tabs('close', '销售订单变更');
            var pagetitle = "销售订单变更";
            parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=2&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
            parent.NeedRefreshPage = this;
        }
    }
}
var OrderModifys2 = function (vid, orderCode, type) {
    if (orderCode.indexOf('DZ') >= 0) {
        window.parent.$("#tabs").tabs('close', '赠送订单变更');
        var pagetitle = "赠送订单变更";
        parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=3&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
        parent.NeedRefreshPage = this;
    } else {
        if (type.indexOf('一推') >= 0) {
            window.parent.$("#tabs").tabs('close', '一推订单变更');
            var pagetitle = "一推订单变更";
            parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=3&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
            parent.NeedRefreshPage = this;
        } else {
            window.parent.$("#tabs").tabs('close', '销售订单变更');
            var pagetitle = "销售订单变更";
            parent.addTabs(pagetitle, '../Crm/Sale/AddOrder.aspx?flag=3&id=' + vid + '&entity=CrmCustomerOrder&title=' + encodeURI(pagetitle));
            parent.NeedRefreshPage = this;
        }
    }
}

//订单审核
var fnCheckOrder = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        if (confirm("您确定审核该订单吗?")) {
            var SaveUrl = Url + "?r=" + Math.random() + "&action=update&entity=CrmCustomerOrder&json=&id=" + vid;
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str == "1") {
                            alert("订单审核成功!");
                            RefreshData();
                        }
                        else {
                            alert(str);
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}
//课程转换
var fnOrderSaleModify = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var statename = record.getData("OrderState");
    var vid = record.getData("CustomerId");
    var code = record.getData("Code");
    if (code.indexOf('DZ') >= 0) {
        alert("您当前选择的订单为【赠送订单】,不能进行转换!");
    } else {
        if (statename == "签核中") {
            alert("您当前的订单处于 【签核中】 状态，不能转换!");
            return;
        } else if (statename == "结案") {
            alert("您当前的订单处于 【结案】 状态，不能转换!");
            return;
        } else if (statename == "报废") {
            alert("您当前的订单处于 【报废】 状态，不能转换!");
            return;
        } else {
            if (vid != "") {
                var SaveUrl = Url + "?r=" + Math.random() + "&type=1&action=check&entity=CrmCustomerInfoCheckField&json=&id=" + vid + "&code=" + code;
                YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                    success: function (res) {
                        try {
                            var str = res.responseText;
                            if (str == "1" || str == "2") {
                                OrderSaleModifys(vid);
                            } else if (str == "7") {
                                alert("您当前选中的订单不是销售订单,不能变更!");
                                return;
                            } else if (str == "3") {
                                alert("您当前的订单为 【签核中】 状态，不能变更!");
                                return;
                            } else if (str == "4") {
                                alert("您当前的订单为 【结案】 状态，不能变更!");
                                return;
                            } else if (str == "5") {
                                alert("您当前的订单为 【报废】 状态，不能变更!");
                                return;
                            }
                        }
                        catch (e) {
                        }
                        finally {
                        }
                    },
                    failure: function (error) {
                        alert("数据错误：" + error.statusText);
                    }
                }, null);
            } else {
                alert("当前记录ID为空,不能变更!");
                return;
            }
        }
    }
}
var OrderSaleModifys = function (vid) {
    window.parent.$("#tabs").tabs('close', '课程转换');
    var pagetitle = "课程转换";
    parent.addTabs(pagetitle, '../Crm/Sale/AddOrderSale.aspx?id=' + vid + '&title=' + encodeURI(pagetitle));
    parent.NeedRefreshPage = this;
}

//新增历史订单
var fnAddOldOrder = function () {
    window.parent.$("#tabs").tabs('close', '新增历史订单');
    var pagetitle = "新增历史订单";
    parent.addTabs(pagetitle, '../Crm/BasePages/StartWorkflow.aspx?title=' + encodeURI(pagetitle) + '&WorktaskId=e2364f32-5a97-49c8-8606-e13e466cb515&WorkflowId=b19632e4-d83b-49b4-b7e2-aa9b682ca2df');
    parent.NeedRefreshPage = this;
}

if (typeof ($) == "undefined") { //屏蔽右键
    parent.unContextMenu(document);
}
if (typeof (YAHOO) == "undefined") {
    parent.unContextMenu(document);
}